package com.chuangsi.project.admin.service.impl;


import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuangsi.common.utils.Assert;
import com.chuangsi.common.utils.SecurityUtils;
import com.chuangsi.project.admin.domain.PayOrder;
import com.chuangsi.project.admin.domain.Seal;
import com.chuangsi.project.admin.dto.app.order.ResPayOrderNotFinishDto;
import com.chuangsi.project.admin.dto.app.order.ResPlaceAnOrderDto;
import com.chuangsi.project.admin.dto.app.payorder.OfflinePayOrderDto;
import com.chuangsi.project.admin.dto.app.payorder.OnlinePayOrderDto;
import com.chuangsi.project.admin.mapper.PayOrderMapper;
import com.chuangsi.project.admin.service.IPayOrderService;
import com.chuangsi.project.admin.service.SealService;
import com.chuangsi.project.system.service.ISysUserService;
import com.chuangsi.project.wechat.PayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author ywb
 * @since 2025-02-25
 */
@Service
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder> implements IPayOrderService {

    @Resource
    private PayUtil payUtil;
    @Autowired
    private SealService sealService;
    @Autowired
    private ISysUserService sysUserService;

    @Override
    public void updateOrderstatus(String outTradeNo, Integer status) {
        LambdaUpdateWrapper<PayOrder> wrapper = Wrappers.lambdaUpdate(PayOrder.class);
        wrapper.eq(PayOrder::getOutTradeNo,outTradeNo);
        wrapper.set(PayOrder::getStatus,status);
        if(status.equals(2)){
            wrapper.set(PayOrder::getPayTime,LocalDateTime.now());
        }
        update(wrapper);
    }

    @Override
    public PayOrder generatePayOrder(Integer orderType, String sealId,String productId, BigDecimal money,String openId) {
        PayOrder payOrder = new PayOrder();
        payOrder.setOrderType(orderType);
        payOrder.setSealId(sealId);
        payOrder.setProductId(productId);
        payOrder.setFee(money);
        payOrder.setPrice(money);
        payOrder.setStatus(1);//未支付
        payOrder.setUserId(SecurityUtils.getUserId());
        payOrder.setOutTradeNo(IdUtil.simpleUUID());
        payOrder.setCreateTime(LocalDateTime.now());
//        if(StrUtil.isNotBlank(openId)){
//            SysUser sysUser = sysUserService.selectUserByOpenId(openId);
//            if(sysUser!=null){
//                payOrder.setCustomerUserId(sysUser.getUserId());
//            }
//        }
        payOrder.insert();
        return payOrder;
    }

    @Override
    public List<ResPayOrderNotFinishDto> getNotFinishPayOrder() {
        return baseMapper.getNotFinishPayOrder(SecurityUtils.getUserId());
    }

    @Override
    public void offlinePay(OfflinePayOrderDto payOrderDto) {
        PayOrder payOrder = getById(payOrderDto.getPayOrderId());
        Assert.notNull(payOrder,"未找到支付订单");
        Assert.isTrue(!payOrder.getUserId().equals(SecurityUtils.getUserId()),"该订单不是您发起的，您不能完成支付");
        Assert.isTrue(payOrder.getOrderType() == 1,"该订单是线上支付，不能完成");
        Assert.isTrue(payOrderDto.getOfflinePayPicList() == null || payOrderDto.getOfflinePayPicList().isEmpty(),"请上传支付截图");
        payOrder.setOfflinePayPics(String.join(",",payOrderDto.getOfflinePayPicList()));
        payOrder.setStatus(2);
        payOrder.setPayTime(LocalDateTime.now());
        payOrder.updateById();
    }

    @Override
    public Integer getPayStatus(Long payOrderId) {
        PayOrder payOrder = getOne(Wrappers.lambdaQuery(PayOrder.class).eq(PayOrder::getPayOrderId,payOrderId).select(PayOrder::getStatus));
        Assert.notNull(payOrder,"未找到支付订单");
        return payOrder.getStatus();
    }

    @Override
    public ResPlaceAnOrderDto onlinePay(OnlinePayOrderDto payOrderDto) {
        PayOrder payOrder = getById(payOrderDto.getPayOrderId());
        Assert.notNull(payOrder,"未找到支付订单");
//        SysUser user = sysUserService.selectUserByOpenId(payOrderDto.getOpenId());
//        Assert.notNull(user,"未找到支付用户");
        payOrder.setOpenId(payOrder.getOpenId());
//        payOrder.setCustomerUserId(user.getUserId());
        payOrder.updateById();
        Assert.notNull(payOrder,"未找到支付订单");
        Seal seal = sealService.getById(payOrder.getSealId());
        Assert.notNull(seal,"未找到印章");
        return payUtil.jsapi(payOrder, payOrderDto.getOpenId(), seal);
    }


}

























